﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yafc.Interface.Api;
using System.Net;

namespace Fanfou.Api
{
	class FrndTL : MyTL
	{
		override public ApiResults execute(ApiArgs Args)
		{
			HttpWebRequest request = null;
			HttpWebResponse response = null;

			if (Args.GetType() != typeof(TLArgs))
			{
				throw new Exception("FrndTL.execute(): Expecting arguments of type TLArgs but got " + Args.GetType().ToString());
			}
			TLArgs args = (TLArgs)Args;

			if (Args.username.Length == 0)
			{
				throw new Exception("MyTL.execute(): username must not be empty.");
			}

			string url = URL.FrndTL;
			if (args.username.Length > 0)
			{
				url += "?id=" + args.username;
			}

			// Sending request
			request = (HttpWebRequest)WebRequest.Create(url);
			request.ContentType = "application/x-www-form-urlencoded";
			request.Method = "GET";
			request.Headers.Add("Authorization", "Basic " + Args.authorization);

			try
			{
				response = (HttpWebResponse)request.GetResponse();
			}
			catch (WebException ex)
			{
				response = (HttpWebResponse)ex.Response;
			}
			catch (Exception)
			{
			}

			TLResults results = buildResults(response);
			if (response != null)
			{
				response.Close();
			}
			return results;
		}
	}
}
